<?php

/*
 * @Author Ghazy
 */

class AdminController extends Zend_Controller_Action
{
    //constants
    const MAIL_EXIST = 'mail already exits';
    const NAME_EXIST = 'username already exits';
    const ADD_SUCCESS = 'user added successfully';
    const PASSWORD = 'password does not match';
    const EDIT_SUCCESS = 'Edit user success';
    const PASS_SUCCESS = 'password changed successfuly';

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {        
    }
    

    //********************Add new User*******************
    public function adduserAction()
    {
        //Form
       $form = new Application_Form_AddUser();
       $this->view->form = $form;
       if($this->getRequest()->isPost()){
           if($form->isValid($this->getRequest()->getPost())){
               $values = $form->getValues();
               if($values['password'] == $values['verify']){
                   $user = new UMS_Admin_User();
                   $add_user_chk = $user->adduser($values['mail'], $values['name'], $values['password'],
                           $values['role'], $values['status'], $values['date']);
                   if( $add_user_chk == 0 )
                   {
                       echo self::ADD_SUCCESS;
                   }
                   else
                   {
                       if( $add_user_chk == 1)
                       {
                           echo self::MAIL_EXIST;
                       }
                       else
                       {
                           echo self::NAME_EXIST;
                       }
                   }
               }
               else
               {
                    echo self::PASSWORD;
               }
           }
       }
    }


    //****************delete user***********************
    public function deleteuserAction()
    {
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->getHelper('layout')->disableLayout();
        $id = $_GET['id'];
        $user = new UMS_Admin_User();
        $user->removeUser($id);
      // $this->_redirect('admin/index');
    }

    //****************edit user actions**********************
    public function editAction()
    {
        $id = base64_decode($_GET['id']);
        //retrieve all user's data
       $user_record = TblUser::getUserbyId($id);
       $form =  new Application_Form_EditUser();
            $form->setDefault('name',$user_record['login_name']);
            $form->setDefault('mail',$user_record['email']);
            $form->setDefault('date',$user_record['password_exp_date']);

       $role = TblRole::getRolebyId($user_record['role_id']);
            $form->setDefault('role',$role['name']);
            $form->setDefault('status',$user_record['status']);

        $hiden_id = new Zend_Form_Element_Hidden('id');
        $form->addElement($hiden_id);
        $form->setDefault('id',$id);

        //update
        if($this->getRequest()->isPost()){
           if($form->isValid($this->getRequest()->getPost())){

               $values = $form->getValues();
               $user_update = new UMS_Admin_User();
               $edit_user_chk = $user_update->editUser($values['id'], $values['mail'],
                       $values['name'],$values['role'], $values['status'], $values['date']);
               if( $edit_user_chk == 3 )
                   {
                       echo self::EDIT_SUCCESS;
                   }
                   else
                   {
                       if( $edit_user_chk == 1)
                       {
                           echo self::MAIL_EXIST;
                       }
                       else
                       {
                           echo self::NAME_EXIST;
                       }
                   }
           }
        }
        $this->view->form = $form;
        $this->view->id = base64_encode($id);
    }


    public function passwordAction()
    {
        $id = base64_decode($_GET['id']);
        $form = new Application_Form_PasswordChange();
        $this->view->form = $form;
        $user = new UMS_Admin_User();
        $hiden_id = new Zend_Form_Element_Hidden('id');
        $form->addElement($hiden_id);
        $form->setDefault('id',$id);

        if($this->getRequest()->isPost()){
           if($form->isValid($this->getRequest()->getPost())){
               $values = $form->getValues();
               if( $values['newpassword'] == $values['verify'] ){
                   TblUser::changePassword( $values['id'], md5($values['newpassword']) ) ;
                   echo self::PASS_SUCCESS;
               }
               else
               {
                   echo self::PASSWORD;
               }
           }
       }
    }

    //********************ADD Permission********************
    public function addpermAction()
    {
        $permission = new UMS_Admin_Permission();
        $form = new Application_Form_AddPermission();
        $this->view->form = $form;

        if($this->getRequest()->isPost()){
           if($form->isValid($this->getRequest()->getPost())){
               $values = $form->getValues();
               $permission->addNewPermission($values['name'], $values['roles'], $values['resources']);
               //redirect to permission action
           }
       }
    }

    public function deletepermAction()
    {
        $id = base64_decode($_GET['id']);
        $permission = new UMS_Admin_Permission();
        $permission->deletePermission($id);
        //redirect to permission action this will be the permission Tab
    }

    public function editpermAction()
    {
        $permission = new UMS_Admin_Permission();
        $form = new Application_Form_EditPermission();
        $this->view->form = $form;
        $id = base64_decode($_GET['id']);

        $hiden_id = new Zend_Form_Element_Hidden('id');
        $form->addElement($hiden_id);
        $form->setDefault('id',$id);

        if($this->getRequest()->isPost()){
           if($form->isValid($this->getRequest()->getPost())){
               $values = $form->getValues();
               $permission->changePermissionName($values['id'], $values['name']);
               //redirect to permission action
           }
       }
    }


}